import request from '@/utils/request'
import type { ApiResponse } from '@/types'

export interface LostItem {
  lostId: string
  userId: string
  title: string
  description: string
  pic1?: string
  pic2?: string
  pic3?: string
  type: string
  createTimestamp: number
  updateTimestamp: number
}

export interface LostQuery {
  pageNo: number
  pageSize: number
  sortBy?: string
  isAsc?: boolean
  endKey?: string
  startTimestamp?: number
  str?: string
  type?: string  // 修改为string类型
}

export interface LostDto {
  title: string
  description: string
  pic1?: string
  pic2?: string
  pic3?: string
  type: string
}

export interface LostUpdateDto {
  lostId: string
  title: string
  description: string
  pic1?: string
  pic2?: string
  pic3?: string
}

export interface PageQuery {
  pageNo: number
  pageSize: number
  sortBy?: string
  isAsc?: boolean
  endKey?: string
  startTimestamp?: number
}

export const lostApi = {
  // 首页展示失物 - POST /lost/list
  getList: (params: LostQuery): Promise<ApiResponse<any>> => {
    console.log('发送失物招领请求:', params)
    return request({
      method: 'POST',
      url: '/lost/list',
      data: params
    })
  },

  // 获取单个失物详情 - GET /lost/getOne/{lostId}
  getDetail: (lostId: string): Promise<ApiResponse<LostItem>> => {
    return request.get(`/lost/getOne/${lostId}`)
  },

  // 上传失物 - POST /lost/insert
  add: (data: LostDto): Promise<ApiResponse<any>> => {
    return request({
      method: 'POST',
      url: '/lost/insert',
      data: data
    })
  },

  // 编辑失物描述 - POST /lost/update
  edit: (data: LostUpdateDto): Promise<ApiResponse<any>> => {
    return request({
      method: 'POST',
      url: '/lost/update',
      data: data
    })
  },

  // 删除我的失物 - DELETE /lost/delete/{lostId}
  delete: (lostId: string): Promise<ApiResponse<any>> => {
    return request.delete(`/lost/delete/${lostId}`)
  },

  // 分页查询我的失物 - POST /lost/queryMyLost
  getMyLost: (params: PageQuery): Promise<ApiResponse<any>> => {
    return request({
      method: 'POST',
      url: '/lost/queryMyLost',
      data: params
    })
  }
}